import datetime

from django.db import models
from django.db.models import Max
from office.models import Office
from wxchat.models import WxUserInfo


class PersonInfo(models.Model):
    """客户信息"""
    openid = models.CharField(verbose_name='微信ID', max_length=120)
    name = models.CharField(verbose_name='姓名', max_length=24)
    telephone = models.CharField(verbose_name='电话', max_length=32)
    id_card = models.CharField(verbose_name='身份证号', max_length=32)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '客户信息'
        verbose_name_plural = verbose_name


class CompanyInfo(models.Model):
    """公司信息"""
    AGENCY = (
        (0, '否'),
        (1, '是'),
    )
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)
    name = models.CharField(verbose_name='公司名称', max_length=64)
    contact_person = models.CharField(verbose_name='联系人', max_length=32, blank=True, null=True)
    contact_tel = models.CharField(verbose_name='联系方式', max_length=64, blank=True, null=True)
    office_openid = models.CharField(verbose_name='授权微信ID', max_length=120, blank=True, null=True)
    is_agency = models.IntegerField(verbose_name='第三方代理', default=0, choices=AGENCY)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '公司信息'
        verbose_name_plural = verbose_name
        ordering = ["-id"]


class CompanyPerson(models.Model):
    """公司人员信息"""
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)
    name = models.CharField(verbose_name='姓名', max_length=64)
    company = models.ForeignKey(CompanyInfo, verbose_name='公司名称', on_delete=models.CASCADE, db_constraint=False)
    telephone = models.CharField(verbose_name='联系方式', max_length=64)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '公司员工'
        verbose_name_plural = verbose_name


class PrivateContract(models.Model):
    """私人律师服务合同"""
    code = models.CharField(verbose_name='合同编号', max_length=16, blank=True, null=True)
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)
    name = models.CharField(verbose_name='姓名', max_length=24)
    telephone = models.CharField(verbose_name='电话', max_length=32)
    id_card = models.CharField(verbose_name='身份证号', max_length=32)
    office_name = models.CharField(verbose_name='律师名称', max_length=64, blank=True, null=True)
    office_man = models.CharField(verbose_name='联系人', max_length=16, blank=True, null=True)
    office_man_tel = models.CharField(verbose_name='联系电话', max_length=24, blank=True, null=True)
    office_address = models.CharField(verbose_name='地址', max_length=128, blank=True, null=True)
    office_tel = models.CharField(verbose_name='律所电话', max_length=24, blank=True, null=True)
    start_date = models.DateField(verbose_name='开始时间', blank=True, null=True)
    end_date = models.DateField(verbose_name='截止时间', blank=True, null=True)
    is_success = models.BooleanField(verbose_name='生效确认', default=False)
    is_confirm = models.BooleanField(verbose_name='缴费确认', default=False)
    office_openid = models.CharField(verbose_name='业务人员微信ID', max_length=120, blank=True, null=True)
    money = models.IntegerField(verbose_name='合同金额', default=0)
    # picture = models.ImageField(verbose_name='合同文本', upload_to="contract/", blank=True, null=True)
    agency_name = models.CharField(verbose_name='代理人', max_length=64, blank=True, null=True)
    agency_openid = models.CharField(verbose_name='代理微信ID', max_length=64, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.code

    class Meta:
        verbose_name = '私人服务合同'
        verbose_name_plural = verbose_name
        ordering = ["-add_time"]

    @classmethod
    def get_max_code(cls):
        year = datetime.datetime.now().strftime('%Y')
        max_code = cls.objects.filter(code__startswith=year).aggregate(max_code=Max("code"))
        max_value = max_code['max_code']
        if max_value is None:
            num = '0001'
            code = '{0}-{1}'.format(year, num)
            return code
        else:
            num = str(int(max_value[-4:]) + 1).rjust(4, '0')
            code = '{0}-{1}'.format(year, num)
            return code

    def save(self, *args, **kwargs):

        if self.is_success and self.code is None:
            code = self.__class__.get_max_code()
            self.code = code

        office = Office.objects.first()
        if office:
            self.office_tel = office.telephone

        if self.office_openid and self.office_man is None:
            user = WxUserInfo.objects.filter(openid=self.office_openid).first()
            print(user)
            if user:
                if user.member_role_id == 2:   # 是第三方代理
                    person = CompanyPerson.objects.select_related("company").filter(openid=user.openid).first()
                    if person:
                        office_user = WxUserInfo.objects.filter(openid=person.company.office_openid).first()
                        if office_user:
                            self.office_openid = office_user.openid
                            self.office_man = office_user.name
                            self.office_man_tel = office_user.telephone
                            self.agency_name = user.name
                            self.agency_openid = user.openid
                else:
                    self.office_man = user.name
                    self.office_man_tel = user.telephone

        return super().save(*args, **kwargs)


class CompanyContract(models.Model):
    """公司合同"""
    CATEGORY = (
        (0, '双方合同'),
        (1, '代理合同'),
    )
    code = models.CharField(verbose_name='合同编号', max_length=16, blank=True, null=True)
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)
    name = models.CharField(verbose_name='公司名称', max_length=64)
    # credit_code = models.CharField(verbose_name='信用代码', max_length=24)
    # address = models.CharField(verbose_name='公司地址', max_length=128)
    # legal_person = models.CharField(verbose_name='法人代表', max_length=24)
    # telephone = models.CharField(verbose_name='联系电话', max_length=64)
    contact_person = models.CharField(verbose_name='联系人', max_length=32)
    contact_tel = models.CharField(verbose_name='联系电话', max_length=64)
    office_name = models.CharField(verbose_name='律师名称', max_length=64, blank=True, null=True)
    # office_code = models.CharField(verbose_name='信用代码', max_length=24, blank=True, null=True)
    office_man = models.CharField(verbose_name='联系人', max_length=16, blank=True, null=True)
    office_man_tel = models.CharField(verbose_name='联系电话', max_length=24, blank=True, null=True)
    office_address = models.CharField(verbose_name='地址', max_length=128, blank=True, null=True)
    office_tel = models.CharField(verbose_name='律所电话', max_length=24, blank=True, null=True)
    start_date = models.DateField(verbose_name='开始时间', blank=True, null=True)
    end_date = models.DateField(verbose_name='截止时间', blank=True, null=True)
    is_success = models.BooleanField(verbose_name='生效确认', default=False)
    is_confirm = models.BooleanField(verbose_name='缴费确认', default=False)
    office_openid = models.CharField(verbose_name='业务人员微信ID', max_length=120, blank=True, null=True)
    money = models.IntegerField(verbose_name='合同金额', default=0)
    category = models.IntegerField(verbose_name='合同类型', default=0, choices=CATEGORY)
    # picture = models.ImageField(verbose_name='合同文本', upload_to="contract/", blank=True, null=True)
    agency_name = models.CharField(verbose_name='代理人', max_length=64, blank=True, null=True)
    agency_openid = models.CharField(verbose_name='代理微信ID', max_length=64, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '法律顾问合同'
        verbose_name_plural = verbose_name
        ordering = ["-add_time"]

    @classmethod
    def get_max_code(cls):
        year = datetime.datetime.now().strftime('%Y')
        max_code = cls.objects.filter(code__startswith=year).aggregate(max_code=Max("code"))
        max_value = max_code['max_code']
        if max_value is None:
            num = '0001'
            code = '{0}-{1}'.format(year, num)
            return code
        else:
            num = str(int(max_value[-4:]) + 1).rjust(4, '0')
            code = '{0}-{1}'.format(year, num)
            return code

    def save(self, *args, **kwargs):
        office = Office.objects.first()
        if office:
            if self.office_name is None:
                self.office_name = office.name
                self.office_code = office.credit_code
                self.office_address = office.address
                self.office_tel = office.telephone

        if self.office_openid and self.office_man is None:
            user = WxUserInfo.objects.filter(openid=self.office_openid).first()
            if user:
                if user.member_role_id == 2:        # 第三方代理
                    person = CompanyPerson.objects.select_related("company").filter(openid=user.openid).first()
                    if person:
                        office_user = WxUserInfo.objects.filter(openid=person.company.office_openid).first()
                        if office_user:
                            self.office_openid = office_user.openid
                            self.office_man = office_user.name
                            self.office_man_tel = office_user.telephone
                            self.category = 1
                            self.agency_name = user.name
                            self.agency_openid = user.openid
                else:
                    self.office_man = user.name
                    self.office_man_tel = user.telephone
                    self.category = 0

        if self.is_success:
            amount = ContractAmount.objects.filter(money=self.money).first()
            if amount and amount.category == 1 and self.agency_openid:
                self.is_confirm = True

            office_user = WxUserInfo.objects.filter(openid=self.office_openid).first()
            if office_user and office_user.auto_pass and self.agency_openid is None:
                self.is_confirm = True

            if self.code is None:
                code = self.__class__.get_max_code()
                self.code = code
        return super().save(*args, **kwargs)


class ContractAmount(models.Model):
    """合同金额列表"""
    CATEGORY = (
        (0, '无'),
        (1, '第三方代理'),
    )
    money = models.IntegerField(verbose_name='金额', default=0)
    desc = models.CharField(verbose_name='说明', max_length=256, blank=True, null=True)
    sort = models.IntegerField(verbose_name='排序', default=0)
    category = models.IntegerField(verbose_name='类型', default=0, choices=CATEGORY)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return str(self.money)

    class Meta:
        verbose_name = '合同金额列表'
        verbose_name_plural = verbose_name
        ordering = ['sort']


class PrivateContractAmount(models.Model):
    """
    私人律师合同金额
    """
    money = models.IntegerField(verbose_name='金额', default=0)
    desc = models.CharField(verbose_name='说明', max_length=256, blank=True, null=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return str(self.money)

    class Meta:
        verbose_name = '私人律师合同金额'
        verbose_name_plural = verbose_name
        ordering = ['money']


class CardAgencyPerson(models.Model):
    """发卡授权用户"""
    AGENCY = (
        (0, '否'),
        (1, '是'),
    )
    name = models.CharField(verbose_name='姓名', max_length=24)
    telephone = models.CharField(verbose_name='手机号', max_length=24, blank=True, null=True)
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)
    office_openid = models.CharField(verbose_name='授权微信ID', max_length=120, blank=True, null=True)
    is_agency = models.IntegerField(verbose_name='授权标志', default=0, choices=AGENCY)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return str(self.name)

    class Meta:
        verbose_name = '发卡授权'
        verbose_name_plural = verbose_name

    def save(self, *args, **kwargs):

        user = WxUserInfo.objects.filter(openid=self.openid).first()
        if user:
            if self.is_agency == 1 and user.member_role_id is None:
                user.member_role_id = 4
                user.save()
            else:
                if user.member_role_id == 4:
                    user.member_role_id = None
                    user.save()
        return super().save(*args, **kwargs)


class ConsultCard(models.Model):
    """用户咨询卡"""
    ISUSED = (
        (0, '未用'),
        (1, '已用'),
    )
    code = models.CharField(verbose_name='卡号', max_length=24, blank=True, null=True)
    card_name = models.CharField(verbose_name='卡名称', max_length=64, blank=True, null=True)
    name = models.CharField(verbose_name='姓名', max_length=24, blank=True, null=True)
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)
    agency_openid = models.CharField(verbose_name='代理微信ID', max_length=120, blank=True, null=True)
    is_used = models.IntegerField(verbose_name='状态', choices=ISUSED, default=0)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.code

    class Meta:
        verbose_name = '用户咨询卡'
        verbose_name_plural = verbose_name

    @classmethod
    def get_max_code(cls):
        year = datetime.datetime.now().strftime('ZX%Y')
        max_code = cls.objects.filter(code__startswith=year).aggregate(max_code=Max("code"))
        max_value = max_code['max_code']
        if max_value is None:
            num = '00001'
            code = '{0}{1}'.format(year, num)
            return code
        else:
            num = str(int(max_value[-5:]) + 1).rjust(5, '0')
            code = '{0}{1}'.format(year, num)
            return code

    def save(self, *args, **kwargs):
        if self.code is None:
            code = self.__class__.get_max_code()
            self.code = code
        if self.card_name is None:
            self.card_name = '咨询卡'
        return super().save(*args, **kwargs)


class Contact(models.Model):
    """联系方式"""
    unit_name = models.CharField(verbose_name="单位名称", max_length=120, blank=True, null=True)
    dept_name = models.CharField(verbose_name="部门名称", max_length=64, blank=True, null=True)
    job = models.CharField(verbose_name="职务", max_length=32, blank=True, null=True)
    name = models.CharField(verbose_name="职务", max_length=16)
    office_tel = models.CharField(verbose_name="办公电话", max_length=32, blank=True, null=True)
    telephone = models.CharField(verbose_name="手机", max_length=32, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '联系方式'
        verbose_name_plural = verbose_name
        ordering = ["-id"]
